package org.dlsu.mt.parser.dictionary;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Vector;

import org.dlsu.mt.DictionaryInterface;
import org.dlsu.mt.parser.FStructure;
import org.dlsu.mt.parser.FStructureElement;
import org.dlsu.mt.parser.FStructureElementString;
import org.dlsu.mt.parser.Word;

/**
 * made by LEFT thesis group
 *
 * monolingual dictionary module
 *
 * Dictionary class
 * represents the dictionary, which would serve as the storage for all the information for our monolingual dictionaries
 */

public class Dictionary implements DictionaryInterface
{
	public static String strLang;
	Hashtable<String, DictionaryWord> WordList;
	/**
	 * the hashtable serves as the storage for all the words in the dictionary with all its corresponding informations
	 * takes as key the value of the word
	 */
	 
	//public Dictionary(){
	//}
	
	public Dictionary(String strLang)
	{
		/**
		 * constructor with no parameters
		 * initializes the object by instantiating the hashtable with initially no contents
		 */
		
		this.strLang = strLang;
		
		WordList = new Hashtable();		
				
		load();				
				
	}
	
	public DictionaryWord getLanguage1(String word, String pos)
	{
		String query = null;
		
		try
		{
			//System.out.println("pasok ako dito DDDDDDDDDDDIIIIBBBBAAAAAAAAA");
			//org.dlsu.mt.parser.List listTemp = new org.dlsu.mt.parser.List();
			List listTemp = new List();
			List listTemp2 = new List();
			List listTemp3 = new List();
			String temp = "";
			String temp2 = "";
			String temp3 = "";
			
			//org.dlsu.mt.parser.CollocationList collocationListTemp = new org.dlsu.mt.parser.CollocationList();
			CollocationList collocationListTemp = new CollocationList();
			org.dlsu.mt.parser.dictionary.DictionaryWord dwTemp;
			org.dlsu.mt.parser.dictionary.WordClass wcTemp;
			
			Class.forName("com.mysql.jdbc.Driver");
	        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/leftDB", "root",""); 
   			Statement stmt = con.createStatement();
   			
   			query = "SELECT * FROM language1Dict WHERE word = '" + word + "' AND pos = '" + pos + "' ";
   				
   			ResultSet rs = stmt.executeQuery(query);
   			   			
   			rs.next();
   			
   			temp = rs.getString("semantics");
			temp2 = rs.getString("fschemata");
			temp3 = rs.getString("lfg");
					
			if(temp != null && !(temp.equals("")))
			{
				String[] arr = temp.split(", ");
				int i = 0;
											
				while(i < arr.length)
				{
					listTemp.addElement(arr[i]);
					i++;
				}
			}
					
			if(temp2 != null && !(temp2.equals("")))
			{
				String[] arr2 = temp2.split(";");
				int i2 = 0;
											
				while(i2 < arr2.length)
				{
					listTemp2.addElement(arr2[i2]);
					i2++;
				}
			}
					
			if(temp3 != null && !(temp3.equals("")))
			{
				String[] arr3 = temp3.split(";");
				int i3 = 0;
											
				while(i3 < arr3.length)
				{
					listTemp3.addElement(arr3[i3]);
					i3++;
				}
			}
			
			wcTemp = new WordClass(rs.getString("id"), rs.getString("pos"), listTemp, listTemp2, listTemp3);
						
			dwTemp = new DictionaryWord(word);
			dwTemp.addWordClass(wcTemp);
			
			rs.close();
   			stmt.close();
			con.close();
			
			return dwTemp;
			
		}
		catch(Exception e)
		{
			//System.out.println("pasok ako dito DDDDDDDDDDDIIIIBBBBAAAAAAAAA");
			return null;
		}
	}
	
	public DictionaryWord getLanguage2(String word, String pos)
	{
		String query = null;
		
		try
		{
			//System.out.println("pasok ako dito DDDDDDDDDDDIIIIBBBBAAAAAAAAA");
			//org.dlsu.mt.parser.List listTemp = new org.dlsu.mt.parser.List();
			List listTemp = new List();
			List listTemp2 = new List();
			List listTemp3 = new List();
			String temp = "";
			String temp2 = "";
			String temp3 = "";
			
			//org.dlsu.mt.parser.CollocationList collocationListTemp = new org.dlsu.mt.parser.CollocationList();
			CollocationList collocationListTemp = new CollocationList();
			org.dlsu.mt.parser.dictionary.DictionaryWord dwTemp;
			org.dlsu.mt.parser.dictionary.WordClass wcTemp;
			
			Class.forName("com.mysql.jdbc.Driver");
	        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/leftDB", "root",""); 
   			Statement stmt = con.createStatement();
   			
   			query = "SELECT * FROM language2Dict WHERE word = '" + word + "' AND pos = '" + pos + "' ";
   				
   			ResultSet rs = stmt.executeQuery(query);
   			   			
   			rs.next();
   			
   			temp = rs.getString("semantics");
			temp2 = rs.getString("fschemata");
			temp3 = rs.getString("lfg");
					
			if(temp != null && !(temp.equals("")))
			{
				String[] arr = temp.split(", ");
				int i = 0;
											
				while(i < arr.length)
				{
					listTemp.addElement(arr[i]);
					i++;
				}
			}
					
			if(temp2 != null && !(temp2.equals("")))
			{
				String[] arr2 = temp2.split(";");
				int i2 = 0;
											
				while(i2 < arr2.length)
				{
					listTemp2.addElement(arr2[i2]);
					i2++;
				}
			}
					
			if(temp3 != null && !(temp3.equals("")))
			{
				String[] arr3 = temp3.split(";");
				int i3 = 0;
											
				while(i3 < arr3.length)
				{
					listTemp3.addElement(arr3[i3]);
					i3++;
				}
			}
			
			wcTemp = new WordClass(rs.getString("id"), rs.getString("pos"), listTemp, listTemp2, listTemp3);
			
			dwTemp = new DictionaryWord(word);
			dwTemp.addWordClass(wcTemp);
			
			rs.close();
   			stmt.close();
			con.close();
			
			return dwTemp;
			
		}
		catch(Exception e)
		{
			//System.out.println("pasok ako dito DDDDDDDDDDDIIIIBBBBAAAAAAAAA");
			return null;
		}
	}
	
	public void load()
	{
		org.dlsu.mt.parser.dictionary.List listTemp, listTemp2, listTemp3;
		org.dlsu.mt.parser.dictionary.CollocationList collocationListTemp;
		org.dlsu.mt.parser.dictionary.DictionaryWord dwTemp;
		org.dlsu.mt.parser.dictionary.WordClass wcTemp;
		
		String strTempWord, strTempPOS, strTempSem, strTempCol;
		//boolean bCheck = true;
		
		String query = null;
		
		try
		{
			
			Class.forName("com.mysql.jdbc.Driver");
	        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/leftDB", "root","");
			
   			Statement stmt = con.createStatement();
   			
   			if (strLang.equals("Language1"))
   				query = "SELECT * FROM language1Dict";
   			else
   				query = "SELECT * FROM language2Dict";
   				
   			ResultSet rs = stmt.executeQuery(query);
   			
   			//Vector vTemp = new Vector();
   					
			try
			{
				while(rs.next())
				{
					listTemp = new org.dlsu.mt.parser.dictionary.List();
					listTemp2 = new org.dlsu.mt.parser.dictionary.List();
					listTemp3 = new org.dlsu.mt.parser.dictionary.List();
					collocationListTemp = new org.dlsu.mt.parser.dictionary.CollocationList();
				
					String temp = "";
					String temp2 = "";
					String temp3 = "";
					
					temp = rs.getString("semantics");
					temp2 = rs.getString("fschemata");
					temp3 = rs.getString("lfg");
					
					if(temp != null && !(temp.equals("")))
					{
						String[] arr = temp.split(", ");
						int i = 0;
											
						while(i < arr.length)
						{
							listTemp.addElement(arr[i]);
							i++;
						}
					}
					
					if(temp2 != null && !(temp2.equals("")))
					{
						String[] arr2 = temp2.split(";");
						int i2 = 0;
											
						while(i2 < arr2.length)
						{
							listTemp2.addElement(arr2[i2]);
							i2++;
						}
					}
					
					if(temp3 != null && !(temp3.equals("")))
					{
						String[] arr3 = temp3.split(";");
						int i3 = 0;
											
						while(i3 < arr3.length)
						{
							listTemp3.addElement(arr3[i3]);
							i3++;
						}
					}
					
					wcTemp = new WordClass(rs.getString("id"), rs.getString("pos"), listTemp, listTemp2, listTemp3);
											
					dwTemp = new DictionaryWord(rs.getString("word"));
					dwTemp.addWordClass(wcTemp);
					addWord(dwTemp);
					
					//vTemp.addElement(rs.getString( "rule" ));	
				}
				rs.close();
				
			} catch (SQLException sqle )
			{	
				System.out.println(sqle.getMessage());
			}
						
			stmt.close();
			con.close();
			System.out.println(strLang.toUpperCase() + " DICTIONARY LOADING SUCCESSFUL!");
			
		}catch(Exception e){
			System.out.println("ERROR LOADING DICTIONARY");
			//System.out.println(e.getMessage());
		}					

	}
	
	public void addWord(DictionaryWord w)
	{
		/**
		 * adds a word object into the dictionary
		 * if the object is already contained in the dictionary, it is merged with the existing word
		 */
		if(WordList.containsKey(w.getValue()))
		{
			WordList.get(w.getValue()).mergeWord(w);
		}
		else
		{	
			WordList.put(w.getValue(), w);
		}
	}
	
	public boolean removeWord(String word)
	{
		/**
		 * removes a word from the dictionary by specifying the value of the word
		 *
		 * returns true if remove is successful
		 * returns false otherwise
		 */
		if(WordList.remove(word) != null)
			return true;
		else
			return false;
	}
	
	public DictionaryWord getWord(String word)
	{
		/**
		 * returns the word object by indicating value of the word
		 * returns null if the word is not found in the dictionary
		 */
		return WordList.get(word);
	}
	
	public FStructureElement lookup(String inputWord, String pos)
	{
		//System.out.println(inputWord);
		/**
		 * to be implemented in the future
		 */
		//return null;

		DictionaryWord dw;
		dw = WordList.get(inputWord);
		
		WordClass wc;
		Word word = new Word(inputWord);
		
		//Vector posTags = new Vector();
		//String[] strArr = dw.getAllPOS();
		String temp;
		
		HashSet hs;
		
		FStructure f = new FStructure(dw.getValue());
		
		int j = 0;
		
		hs = new HashSet();
			
		wc = dw.getClass(pos);
		int u = 0;
		while(u < wc.getCtr())
		{
			hs.add(new FStructureElementString("SEM" + "\t" + wc.getNextSemantics()));
			u++;
		}
		
		int v = 0;
		while(v < wc.getFSCtr())
		{
			hs.add(new FStructureElementString("FUNC" + "\t" + wc.getNextFS()));
			v++;
		}
			
		int k = 0;
		while(k < wc.getLFGCtr())
		{
			String[] str = wc.getNextLFG().split("=");
			String[] str2 = str[0].split(" ");
			String strTemp = "";
			
			int q = 0;
			while(q < (str2.length-1))
			{
				strTemp = strTemp + str2[q] + " ";
				q++;
			}
			
			strTemp = strTemp + str2[str2.length-1].toUpperCase();
			
			hs.add(new FStructureElementString(strTemp + "\t" + str[1]));
			k++;
		}
		
		FStructureElement fseOrig = new FStructureElement(wc.getPOS(), hs);
		
		hs.add(new FStructureElementString("PRED" + "\t" + inputWord));
		
		//f.addEntry(fseOrig);		
			
	
		//FStructure f = new FStructure(word.getRootWord());
		//word.addPOSTagEntry(inputWord, f);
		//return word;
		
		//System.out.println(inputWord);
		
		/*
		int nLen = strArr.length;
		try
		{
			int n = 0;
			while(n < nLen)
			{
				posTags.addElement(strArr[n]);
				n++;
			}
			
			//word.setF(f, hs);	
		}
		catch(Exception e)
		{
			//System.out.println("sa catch");
			posTags = null;
		};

		if (posTags != null)
		{
			for (int i =0; i< posTags.size(); i++) {
				word.addPOSTagEntry((String)posTags.elementAt(i), f);
			}
			return word;
		}*/
			
		return fseOrig;
	}
	
	public Word lookup(Word word)
	{
		/**
		 * to be implemented in the future
		 */
		return null;
	}
	
	public Word lookup(String inputWord)
	{
		
		/**
		 * to be implemented in the future
		 */
		//return null;

		DictionaryWord dw;
		dw = WordList.get(inputWord);
		
		WordClass wc;
		Word word = new Word(inputWord);
		
		Vector posTags = new Vector();
		
		//GIANAHOO System.out.println("Input word is " + inputWord);
		/*GIANAHOO
		if (dw == null)
			System.out.println("NULL");
		*/
		String[] strArr = dw.getAllPOS();		
		String temp;
		
		HashSet hs;
		
		FStructure f = new FStructure(dw.getValue());
		
		int j = 0;
		while(j < strArr.length)
		{
			hs = new HashSet();
			
			//START COPYING FROM HERE
			wc = dw.getClass(strArr[j]);
			int u = 0;
			while(u < wc.getCtr())
			{
				hs.add(new FStructureElementString("SEM" + "\t" + wc.getNextSemantics()));
				u++;
			}
		
			int v = 0;
			while(v < wc.getFSCtr())
			{
				hs.add(new FStructureElementString("FUNC" + "\t" + wc.getNextFS()));
				v++;
			}
			
			int k = 0;
			while(k < wc.getLFGCtr())
			{
				String[] str = wc.getNextLFG().split("=");
				String[] str2 = str[0].split(" ");
				String strTemp = "";
				
				int q = 0;
				while(q < (str2.length-1))
				{
					strTemp = strTemp + str2[q] + " ";
					q++;
				}
				
				strTemp = strTemp + str2[str2.length-1].toUpperCase();
				
				hs.add(new FStructureElementString(strTemp + "\t" + str[1]));
				k++;
			}
			
			FStructureElement fseOrig = new FStructureElement(wc.getPOS(), hs);
			
			hs.add(new FStructureElementString("PRED" + "\t" + inputWord));
			f.addEntry(fseOrig);
			j++;
		}	
	
		//FStructure f = new FStructure(word.getRootWord());
		//word.addPOSTagEntry(inputWord, f);
		//return word;
		
		//System.out.println(inputWord);
		int nLen = strArr.length;
		try
		{
			int n = 0;
			while(n < nLen)
			{
				posTags.addElement(strArr[n]);
				n++;
			}
			
			//word.setF(f, hs);	
		}
		catch(Exception e)
		{
			//System.out.println("sa catch");
			posTags = null;
		};

		if (posTags != null)
		{
			for (int i =0; i< posTags.size(); i++) {
				word.addPOSTagEntry((String)posTags.elementAt(i), f);
			}
			return word;
		}
			
		return null;
	}
		
	public String getId(String word, String POS){
		
		DictionaryWord dw = getWord(word);
		
		WordClass wc = dw.getClass(POS);
		
		return wc.getId();		
	}	
}	